package com.yz.jdd.module.cashier;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;


import com.yz.jdd.base.entity.Response_Base;
import com.yz.jdd.base.entity.Template;
import com.yz.jdd.base.utils.BaseConfigs;
import com.yz.jdd.base.utils.IntentHandler;
import com.yz.jdd.base.utils.PreferencesUtils;
import com.yz.jdd.network.ApiCallback;
import com.yz.jdd.network.response.Response_BalTemp;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


/**
 * Created by shibin on 2017/1/16.
 * 收银台
 */

public class CashierPresenter implements CashierContract.Presenter {
    private CashierContract.CashierView iCashierView;//view层
    private CashierModelImpl cashierModel;//model层

    private String oldMoney;

    public CashierPresenter(CashierContract.CashierView iCashierView, String closeOrderReplay) {
        oldMoney = closeOrderReplay;
        this.iCashierView = iCashierView;
        iCashierView.setPresenter(this);
        cashierModel = new CashierModelImpl();
    }

    /**
     * 处理edittext && 最多保留小数点后两位
     * 总金额输入禁止0 0.0 0.00金额
     *
     * @param isTotal 是否是输入的总金额
     * @return
     */
    @Override
    public TextWatcher textWatcher(final boolean isTotal) {
        TextWatcher textWatcher = new TextWatcher() {
            private StringBuffer str_buf_;//缓存当前的string，用以修改内容

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                str_buf_ = new StringBuffer(s.toString().trim());
                if (s.length() > 0) {
                    iCashierView.setTextSize(40, isTotal);
                } else {
                    iCashierView.setTextSize(18, isTotal);
                }
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0, s.toString().indexOf(".") + 3);
                        iCashierView.setTextInput(isTotal, s.toString());
                        iCashierView.setSelection(isTotal, s.length());
                    }
                }
                if (s.toString().trim().equals(".")) {
                    iCashierView.setTextInput(isTotal, "");
                    return;
                }

                if (s.toString().startsWith("0") && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        iCashierView.setTextInput(isTotal, s.subSequence(0, 1).toString());
                        iCashierView.setSelection(isTotal, 1);
                        return;
                    }
                }
//                Float.valueOf(str_buf_.toString()) >9999999.99

                if (!TextUtils.isEmpty(str_buf_.toString()) && new BigDecimal(str_buf_.toString()).compareTo(new BigDecimal("999999.99")) > 0) {
                    str_buf_.deleteCharAt(start);
                    iCashierView.setTextInput(isTotal, str_buf_.toString());
                    iCashierView.setSelection(isTotal, str_buf_.length());
                    return;
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                iCashierView.onText(isTotal, s.toString());
                if (isTotal) {
                    if (!TextUtils.isEmpty(s)) {
                        try {
                            if (!".".equals(s.toString())) {
                                if (s.toString().contains(".")) {
                                    if (s.length() - 1 - s.toString().indexOf(".") <= 2) {
                                        if (0 != Float.valueOf(s.toString())) {
                                            iCashierView.isActive(true);
                                        } else {
                                            iCashierView.isActive(false);
                                        }
                                    } else {
                                        return;
                                    }
                                } else {
                                    if (0 != Float.valueOf(s.toString())) {
                                        if (s.toString().startsWith("0")) {
                                            iCashierView.isActive(false);
                                        } else {
                                            iCashierView.isActive(true);
                                        }
                                    } else {
                                        iCashierView.isActive(false);
                                    }
                                }
                            } else {
                                iCashierView.isActive(false);
                            }
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        }
                    } else {
                        iCashierView.isActive(false);
                    }
                }
            }
        };
        return textWatcher;
    }


    /**
     * 处理edittext && 最多保留小数点后两位
     * 总金额输入禁止0 0.0 0.00金额
     *
     * @return
     */
    @Override
    public TextWatcher textEarseWatcher(final float totalAmount) {
        TextWatcher textWatcher = new TextWatcher() {
            private StringBuffer str_buf_;//缓存当前的string，用以修改内容

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                str_buf_ = new StringBuffer(s.toString().trim());
//                if (s.length() > 0) {
//                    iCashierView.setTextSize(40, isTotal);
//                } else {
//                    iCashierView.setTextSize(18, isTotal);
//                }
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 3);
                        iCashierView.setTextInput(false, s.toString());
                        iCashierView.setSelection(false, s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    iCashierView.setTextInput(false, "");
                    return;
                }

                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        iCashierView.setTextInput(false, s.subSequence(0, 1).toString());
                        iCashierView.setSelection(false, 1);
                        return;
                    }
                }
                if (!TextUtils.isEmpty(str_buf_.toString()) && Float.valueOf(str_buf_.toString()) >= totalAmount) {
                    str_buf_.deleteCharAt(start);
                    iCashierView.setTextInput(false, str_buf_.toString());
                    iCashierView.setSelection(false, str_buf_.length());
                    return;
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s)) {
                    try {
                        if (!".".equals(s.toString())) {
                            if (s.toString().contains(".")) {
                                if (s.length() - 1 - s.toString().indexOf(".") <= 2) {
                                    if (0 != Float.valueOf(s.toString())) {
                                        iCashierView.isActive(true);
                                    } else {
                                        iCashierView.isActive(false);
                                    }
                                } else {
                                    return;
                                }
                            } else {
                                if (0 != Float.valueOf(s.toString())) {
                                    if (s.toString().startsWith("0")) {
                                        iCashierView.isActive(false);
                                    } else {
                                        iCashierView.isActive(true);
                                    }

                                } else {
                                    iCashierView.isActive(false);
                                }
                            }
                        } else {
                            iCashierView.isActive(false);
                        }

                    } catch (NumberFormatException e) {
                        e.printStackTrace();

                    }
                } else {
                    iCashierView.isActive(false);
                }
            }

        };
        return textWatcher;
    }


    /**
     * 充值送活动
     */
    @Override
    public void doGetPromotions(final Context context, final String totalAmount) {

    }

    /**
     * 商户优惠活动 （充值送活动or送券活动）
     *
     * @param context
     * @param totalAmount
     * @Tips: 1.是否开启会员卡且有充值返利活动 否就判断是否有送券活动 开启了会员就判断是否有可参与活动
     */
    @Override
    public void martchPromotions(Context context, final String totalAmount) {

    }


}
